<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="工序名称" prop="processName">
        <el-input
          v-model="queryParams.processName"
          placeholder="请输入工序名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
   <el-form-item label="模块" prop="moduleId">
     <el-select v-model="queryParams.moduleId" filterable @change="handleChangeQueryModule" placeholder="请选择模块">
       <el-option v-for="item in queryModuleOptions" :key="item.moduleId" :label="item.moduleName"
         :value="item.moduleId" :disabled="item.status == 1"></el-option>
     </el-select>
   </el-form-item>
     <el-form-item label="所属车间" prop="workshopId">
       <el-select v-model="queryParams.workshopId" filterable @change="handleChangeQueryWorkshop" placeholder="请选择所属车间">
         <el-option v-for="item in queryWorkshopOptions" :key="item.workshopId" :label="item.workshopName"
           :value="item.workshopId" :disabled="item.status == 1"></el-option>
       </el-select>
     </el-form-item>
     <el-form-item label="大工序" prop="gprocessId">
       <el-select v-model="queryParams.gprocessId" filterable placeholder="请选择大工序">
         <el-option v-for="item in queryGprocessOptions" :key="item.gprocessId" :label="item.gprocessName"
           :value="item.gprocessId" :disabled="item.status == 1"></el-option>
       </el-select>
     </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sl:process:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sl:process:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sl:process:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="processList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工序名称" align="center" prop="processName" />
      <el-table-column label="编码" align="center" prop="processCode" />
      <el-table-column label="计价" align="center" prop="processPay" />
      <el-table-column label="定量" align="center" prop="quantify" />
      <el-table-column label="所属模块" align="center"  prop="module.moduleName"/>
      <el-table-column label="所属车间" align="center" prop="workshop.workshopName" />
      <el-table-column label="所属大工序" align="center"  prop="gprocess.gprocessName"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sl:process:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sl:process:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工序对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="工序名称" prop="processName">
              <el-input v-model="form.processName" placeholder="请输入工序名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="编码" prop="processCode">
              <el-input v-model="form.processCode" placeholder="请输入编码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属模块" prop="moduleId">
                <el-select v-model="form.moduleId" @change="handleChangeModule" placeholder="请选择所属模块">
                  <el-option
                    v-for="item in moduleOptions"
                    :key="item.moduleId"
                    :label="item.moduleName"
                    :value="item.moduleId"
                    :disabled="item.status == 1"
                  ></el-option>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属车间" prop="workshopId">
                <el-select v-model="form.workshopId" @change="handleChangeWorkshop" placeholder="请选择所属车间">
                  <el-option
                    v-for="item in workshopOptions"
                    :key="item.workshopId"
                    :label="item.workshopName"
                    :value="item.workshopId"
                    :disabled="item.status == 1"
                  ></el-option>
                </el-select>
            </el-form-item>
          </el-col>
        </el-row>
       <el-row>
         <el-col :span="12">
           <el-form-item label="所属大工序" prop="gprocessId">
               <el-select v-model="form.gprocessId" placeholder="请选择所属大工序">
                 <el-option
                   v-for="item in gprocessOptions"
                   :key="item.gprocessId"
                   :label="item.gprocessName"
                   :value="item.gprocessId"
                   :disabled="item.status == 1"
                 ></el-option>
               </el-select>
           </el-form-item>
         </el-col>
         <el-col :span="12">
           <el-form-item label="计价" prop="processPay">
             <el-input class="def-input-number" v-model="form.processPay" type="number" placeholder="请输入计价" >
              <template slot="append">元/件</template>
             </el-input>
           </el-form-item>
         </el-col>
       </el-row>
       <el-row>
         <el-col :span="12">
           <el-form-item label="定量" prop="quantify">
                <el-input v-model="form.quantify" class="def-input-number" type="number" placeholder="请输入定量" />
            </el-form-item>
          </el-col>
        </el-row>
         <el-form-item label="是否平分" prop="isBisect">
            <el-radio-group v-model="form.isBisect">
              <el-radio
                v-for="dict in dict.type.sl_process_bisect"
                :key="dict.value"
                :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        <el-form-item label="显示顺序" prop="processSort">
          <el-input-number v-model="form.processSort" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_normal_disable"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProcess, getProcess, delProcess, addProcess, updateProcess } from "@/api/sl/process";
import { getWorkshopListByModuleId } from "@/api/sl/workshop";
import { getGprocessListByWorkshopId } from "@/api/sl/gprocess";
import {allModule} from "@/api/sl/module";

export default {
  name: "Process",
  dicts: ['sys_normal_disable','sl_process_bisect'],
  data() {
    let watermarkContentVaild = (rule, value, callback) => {
      if (value == null || value === "") {
        callback();
      } else {
        let length = value.length
        if (length > 7) {
          callback(new Error("输入不能超过7位"))
        } else {
          callback();
        }
      }
    }
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工序表格数据
      processList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        processName: null,
        processCode: null,
        status: null,
        moduleId: null,
        workshopId: null,
        gprocessId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        processName: [
          { required: true, message: "工序名称不能为空", trigger: "blur" }
        ],
        processCode: [
          { required: true, message: "编码不能为空", trigger: "blur" }
        ],
        processPay: [
          { required: true, message: "计价不能为空", trigger: "blur" },
          {validator: watermarkContentVaild, trigger: 'blur'}
        ],
        processSort: [
          { required: true, message: "显示顺序不能为空", trigger: "blur" }
        ],
        moduleId: [
          { required: true, message: "所属模块不能为空", trigger: "blur" }
        ],
        workshopId: [
          { required: true, message: "所属车间不能为空", trigger: "blur" }
        ],
        gprocessId: [
          { required: true, message: "所属大工序不能为空", trigger: "blur" }
        ],
        quantify: [
          {validator: watermarkContentVaild, trigger: 'blur'}
        ],
      },
      moduleOptions: [],
      workshopOptions: [],
      gprocessOptions: [],
      //模块
      queryModuleOptions: [],
      //车间
      queryWorkshopOptions: [],
      //大工序
      queryGprocessOptions: [],
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.getModuleList();
  },
  methods: {
    /** 查询工序列表 */
    getList() {
      this.loading = true;
      listProcess(this.queryParams).then(response => {
        this.processList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    getModuleList(){
      allModule().then(response => {
        this.queryModuleOptions = response.data;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        processId: null,
        processName: null,
        processCode: null,
        processPay: null,
        processSort: 0,
        status: "0",
        moduleId: null,
        workshopId: null,
        gprocessId: null,
        remark: null,
        quantify: null,
        isBisect: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.processId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      getProcess().then(response => {
        this.moduleOptions = response.modules;
        this.open = true;
        this.title = "添加工序";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const processId = row.processId || this.ids
      getProcess(processId).then(response => {
        this.form = response.data;
        this.moduleOptions = response.modules;
        this.workshopOptions = response.workshops;
        this.gprocessOptions = response.gprocesss;
        this.open = true;
        this.title = "修改工序";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.processId != null) {
            updateProcess(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProcess(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const processIds = row.processId || this.ids;
      this.$modal.confirm('是否确认删除工序编号为"' + processIds + '"的数据项？').then(function() {
        return delProcess(processIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sl/process/export', {
        ...this.queryParams
      }, `process_${new Date().getTime()}.xlsx`)
    },
    //模块变更查询车间
    handleChangeModule(value){
      //清空车间
      this.form.workshopId = null;
      //清空大工序
      this.form.gprocessId = null;
      getWorkshopListByModuleId(value).then(response => {
        this.workshopOptions = response.data;
      });
    },
    //车间变更查询大工序
    handleChangeWorkshop(value){
      //清空车间
      this.form.gprocessId = null;
      getGprocessListByWorkshopId(value).then(response => {
        this.gprocessOptions = response.data;
      });
    },
    //模块变更查询车间
    handleChangeQueryModule(value) {
      //清空车间
      this.queryWorkshopOptions = [];
      this.queryParams.workshopId = null;
      //清空大工序
      this.queryGprocessOptions = [];
      this.queryParams.gprocessId = null;
      getWorkshopListByModuleId(value).then(response => {
        this.queryWorkshopOptions = response.data;
      });
    },
    //车间变更查询大工序
    handleChangeQueryWorkshop(value) {
      //清空大工序
      this.queryGprocessOptions = [];
      this.queryParams.gprocessId = null;
      getGprocessListByWorkshopId(value).then(response => {
        this.queryGprocessOptions = response.data;
      });
    },
  }
};
</script>
